package com.yc.navy.dayfresh.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.dayfresh.bean.MemberInfo;
import com.yc.navy.dayfresh.dto.CartInfoDTO;
import com.yc.navy.dayfresh.enums.ResultEnum;
import com.yc.navy.dayfresh.service.ICartInfoService;
import com.yc.navy.dayfresh.util.ConstantInfo;
import com.yc.navy.dayfresh.util.StringUtil;
import com.yc.navy.dayfresh.vo.ResultVO;

@RestController
@RequestMapping("/cart")
public class CartInfoController {
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	@Autowired
	private ICartInfoService cartInfoService;
	
	private String getLoginUser(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.CURRENTLOGINMEMBERINFO);
		if(obj == null) {
			return null;
		}
		MemberInfo mf = (MemberInfo) obj;
		return "m_" + mf.getMno();
	}
	
	@PostMapping("/add")
	public ResultVO addCart (HttpSession session, String pno, Integer num) {
		String mno = this.getLoginUser(session);
		if(StringUtil.checkNull(mno)) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			pno = "p_" + pno;
			if(redisTemplate.opsForHash().hasKey(mno, pno)) {//说明该商品购物车在已经存在
				redisTemplate.opsForHash().increment(mno, pno, num);
			}else {
				redisTemplate.opsForHash().put(mno, pno, num);
			}
			return new ResultVO(ResultEnum.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@GetMapping("/finds")
	public ResultVO finds (HttpSession session) {
		String mno = this.getLoginUser(session);
		if(StringUtil.checkNull(mno)) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			Map<Object, Object> map = redisTemplate.opsForHash().entries(mno);
			if(map == null || map.isEmpty()) {
				return new ResultVO(ResultEnum.DATA_NULL);
			}
			return new ResultVO(ResultEnum.SUCCESS,map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@GetMapping("/findList")
	public ResultVO findList(HttpSession session) {
		String mno = this.getLoginUser(session);
		if(StringUtil.checkNull(mno)) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			Map<Object, Object> map = redisTemplate.opsForHash().entries(mno);//
			if(map == null || map.isEmpty()) {
				return new ResultVO(ResultEnum.DATA_NULL);
			}
			
			/*List<String> pnos = new ArrayList<String>();
			 * map.keySet().forEach(key ->{
			 * 	pnos.add(String,valueOf(key).replaceFirst("p_",""));
			 * })
			 * 
			 * */
			
			List<String> pnos = map.keySet().stream().map(item -> String.valueOf(item).replaceFirst("p_", "")).collect(Collectors.toList());
			List<CartInfoDTO> list = cartInfoService.findByPnos(pnos);
			list.forEach(item ->{
				item.setNum(Integer.parseInt(String.valueOf(map.get("p_" + item.getPno()))));
			});
			return new ResultVO(ResultEnum.SUCCESS, list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@PostMapping("/del")
	public ResultVO del (HttpSession session, String pno) {
		String mno = this.getLoginUser(session);
		if(StringUtil.checkNull(mno)) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			redisTemplate.opsForHash().delete(mno, "p_" + pno);
			return new ResultVO(ResultEnum.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
	
	@PostMapping("/findByPnos")
	public ResultVO findByPnos(HttpSession session, String pnos) {
		String mno = this.getLoginUser(session);
		if(StringUtil.checkNull(mno)) {
			return new ResultVO(ResultEnum.LOGIN_ERROR);
		}
		
		try {
			Map<Object, Object> map = redisTemplate.opsForHash().entries(mno);
			if(map == null || map.isEmpty()) {
				return new ResultVO(ResultEnum.DATA_NULL);
			}
			String [] arrs = pnos.split(",");
			List<CartInfoDTO> list = cartInfoService.findByPnos(Arrays.asList(arrs));
			list.forEach(item ->{
				item.setNum(Integer.parseInt(String.valueOf(map.get("p_" + item.getPno()))));
			});
			return new ResultVO(ResultEnum.SUCCESS,list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ResultVO(ResultEnum.ERROR);
	}
}
